﻿namespace dotnet_notes.Controllers
{
    /// <summary>
    /// 文件管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FileController : ControllerBase
    {
        /// <summary>
        /// 获取验证码图片
        /// </summary>
        [HttpPost]
        public IActionResult ValidateCodeImage()
        {
            string randomNumber = RandomTool.GenerateNumber(4).ToString();
            var fs = ImageTool.GenerateValidateCode(randomNumber);
            return File(fs, "image/jpeg");
        }

        /// <summary>
        /// 获取二维码图片
        /// </summary>
        [HttpPost]
        public IActionResult QrCodeImage()
        {
            //string data = new { id = 1, guid = Guid.NewGuid() }.ToJson();
            string url = "https://gitee.com/hechenyu_youya_code";
            var fs = ImageTool.GenerateQrCode(url);
            return File(fs, "image/jpeg");
        }

        /// <summary>
        /// 批量上传文件
        /// </summary>
        /// <param name="formFiles">文件集合</param>
        /// <param name="module">文件存放的模块文件夹名</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> UploadFiles
            (IFormFileCollection formFiles, string module = BusinessConfig.DefaultModule)
        {
            string validModule = BusinessTool.CheckModuleName(module);
            string basePath = Path.Combine(FileTool.GetWwwrootPath(), "uploads", validModule);

            Directory.CreateDirectory(basePath); // 直接创建即可, 如果目录存在不会进行任何操作

            var webPaths = new ArrayList();

            foreach (var formFile in formFiles)
            {
                if (formFile.Length == 0) continue;

                if (ImageTool.CheckSuffix(formFile.FileName) || 
                    FileTool.CheckSuffix(formFile.FileName))
                {
                    string fileName = FileTool.GetFileNameFromTime(Path.GetExtension(formFile.FileName));
                    string filePath = Path.Combine(basePath, fileName);

                    await formFile.SaveToAsync(filePath);
                    webPaths.Add(FileTool.SpliceFilePath("uploads", validModule, fileName));
                }
            }

            return Ok(webPaths);
        }

        /// <summary>
        /// 上传图片生成缩略图
        /// </summary>
        /// <param name="formFile">图片文件</param>
        /// <returns>图片相对访问路径</returns>
        [HttpPost]
        public async Task<IActionResult> ImageToThumbnail(IFormFile formFile)
        {
            if (formFile.Length == 0)
            {
                return BadRequest("图片大小不能为 0");
            }

            if (!ImageTool.CheckSuffix(formFile.FileName))
            {
                return BadRequest("无效的图片类型");
            }

            string basePath = Path.Combine(FileTool.GetWwwrootPath(), "thumbnails");
            string suffix = Path.GetExtension(formFile.FileName);
            string fileName = formFile.FileName.Replace(suffix, "_thumb" + suffix);

            var imageBytes = await formFile.ToArrayAsync();
            ImageTool.SaveAsThumbnail(imageBytes, basePath, fileName);

            return Ok(FileTool.SpliceFilePath("thumbnails", fileName));
        }

        /// <summary>
        /// 上传图片添加时间水印
        /// </summary>
        /// <param name="formFile">图片文件</param>
        /// <returns>图片相对访问路径</returns>
        [HttpPost]
        public async Task<IActionResult> ImageAddTimeWatermark(IFormFile formFile)
        {
            if (formFile.Length == 0)
            {
                return BadRequest("图片大小不能为 0");
            }

            if (!ImageTool.CheckSuffix(formFile.FileName))
            {
                return BadRequest("无效的图片类型");
            }

            string basePath = Path.Combine(FileTool.GetWwwrootPath(), "watermarks");
            string suffix = Path.GetExtension(formFile.FileName);
            string fileName = formFile.FileName.Replace(suffix, "_watermark" + suffix);

            var imageBytes = await formFile.ToArrayAsync();
            ImageTool.AddWatermarkWithTime(imageBytes, basePath, fileName);

            return Ok(FileTool.SpliceFilePath("watermarks", fileName));
        }

        /// <summary>
        /// 上传图片添加文本水印
        /// </summary>
        /// <param name="formFile">图片文件</param>
        /// <returns>图片相对访问路径</returns>
        [HttpPost]
        public async Task<IActionResult> ImageAddTextWatermark(IFormFile formFile)
        {
            if (formFile.Length == 0)
            {
                return BadRequest("图片大小不能为 0");
            }

            if (!ImageTool.CheckSuffix(formFile.FileName))
            {
                return BadRequest("无效的图片类型");
            }

            string basePath = Path.Combine(FileTool.GetWwwrootPath(), "watermarks");
            string suffix = Path.GetExtension(formFile.FileName);
            string fileName = formFile.FileName.Replace(suffix, "_watermark" + suffix);

            var imageBytes = await formFile.ToArrayAsync();
            ImageTool.AddWatermarkWithText(imageBytes, basePath, fileName, "hechenyu");

            return Ok(FileTool.SpliceFilePath("watermarks", fileName));
        }
    }
}
